package Base;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.imageio.ImageIO;

/** 
 * This class inherits like the class Figure from SceneElement and serves for the representation of background objects. 
 * @author Wayang Group Bremen
 * @version 0.9
 */

public class Background extends SceneElement {


	public String path;
	public String path_black;

	// empty constructor
	public Background () {}

	/**
	 * Constructor. Creates an instance of Background.
	 * @param name the title of the background
	 * @param path	the path to the image of the background
	 * @param flipped boolean value that determines if the figure is facing left or right
	 * @param sound boolean value that determines if the figure is making a sound 
	 * @param rotation boolean that determines if the object is rotated
	 * @param Z the Z-buffer, that determines in which layer the object is in.
	 */
	public Background(String name, String path, boolean flipped, boolean sound, boolean rotation, int Z) {
		this.name = name;
		this.text = null;
		this.path = path;
		this.flipped = flipped;
		this.blackwhite = false;
		this.hasFocus = false;
		this.isDragged = false;
		this.animationStates = new Vector<AnimationState>();
		try {
			this.image =  ImageIO.read(new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.rect = new Rectangle(50, 50, image.getWidth(), image.getHeight());
		this.isFigure = false;
	}
	/**
	 * This method returns the name of the figure.
	 * @return title of the figure as String.
	 */
	public String toString() {
		 String tempName="";
		 if(hasFocus) {
			 tempName = "<html><b> "+ name+"</b></html>";
		 }
		 else {
			 tempName = name;
		 }
         return tempName;
	}

	/** 
	 * This method creates paths to the images and the images.
	 */
	public void createImagePaths() {
		String temp = "";
		//remove digits from background name
		for(int t=0;t<name.length();t++) {
			if(!Character.isDigit(name.charAt(t))) {
				temp = temp.concat(Character.toString(name.charAt(t)));
			}
		}
		backGroundPath = "Data//backgroundsicons//" .concat(temp).concat(".png");

		// System.out.println("backgroundPath:" +  backGroundPath);

		if (path_black == null) {
			String appendbw = "bw.";
			Vector<String> image_bw = new Vector<String>();

			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				image_bw.add(st.nextToken());

			}
			image_bw.add(1, appendbw);
			path_black = "";

			for (int i=0; i<image_bw.size();i++) {
				path_black = path_black.concat(image_bw.get(i));
			}
		}
		// System.out.println("path:" + path);
		// System.out.println("pathBlack:" + path_black);
		try {
			this.imageBlack =  ImageIO.read(new File(path_black));
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}
